package com.win.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.win.pojo.Order;
import com.win.properties.OrderProperties;
import com.win.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
//@RefreshScope
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;
    @RequestMapping("/createOrder")
    public Order createOrder(@RequestParam("productId") Long productId,@RequestParam("userId") Long userId){
        Order order = orderService.createOrder(productId,userId);
        return order;
    }


    @Autowired
    LoadBalancerClient loadBalancerClient;

    @RequestMapping("/loadBalancerClientTest")
    public void loadBalancerClientTest(){
        ServiceInstance choose1 = loadBalancerClient.choose("service-product");
        log.info("choose{}:{}",choose1.getHost(),choose1.getPort());
        ServiceInstance choose2 = loadBalancerClient.choose("service-product");
        log.info("choose{}:{}",choose2.getHost(),choose2.getPort());
        ServiceInstance choose3 = loadBalancerClient.choose("service-product");
        log.info("choose{}:{}",choose3.getHost(),choose3.getPort());
        ServiceInstance choose4 = loadBalancerClient.choose("service-product");
        log.info("choose{}:{}",choose4.getHost(),choose4.getPort());
    }

//    @Value("${order.timeout}")
//    String orderTimeout;
//    @Value("${order.auto-confirm}")
//    String orderAutoConfirm;
//
//    @GetMapping("/config")
//    public String getConfig() {
//        return "order.timeout=" + orderTimeout + "；order.auto-confirm=" + orderAutoConfirm;
//    }
    @Autowired
    private OrderProperties orderProperties;
    @GetMapping("/config2")
    public String getConfig2() {
        return "order.timeout=" + orderProperties.getTimeout() +
                "；order.auto-confirm=" + orderProperties.getAutoConfirm()+
                "；order.db_url=" + orderProperties.getDbUrl();
    }


    @RequestMapping("/readDb")
    public String readDb(){
        log.info("readDb……");
        return "readDb success";
    }

    @RequestMapping("/writerDb")
    public String writerDb(){
        log.info("writerDb……");
        return "writerDb success";
    }

    @RequestMapping("/secKill")
    public Order secKill(@RequestParam("productId") Long productId,@RequestParam("userId") Long userId){
        Order order = orderService.createOrder(productId,userId);
        return order;
    }

    @RequestMapping("/secKillParam")
    @SentinelResource(value = "secKillParam-order")
    public Order secKillParam(@RequestParam(value = "productId",required = false) Long productId,@RequestParam(value = "userId",required = false) Long userId){
        Order order = orderService.createOrder(productId,userId);
        return order;
    }

    @GetMapping(value = "/empower")
    public String requestSentinel4(){
        log.info("测试Sentinel授权规则empower");
        return "Sentinel授权规则";
    }
}
